package com.gbr.view.recordingarea;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javafx.application.Platform;

import com.gbr.presentation.recordingarea.IRecordingAreaPresentation;
import com.ng.framework.AbstractPlugin;

public class ViewPlugin extends AbstractPlugin implements PropertyChangeListener {
    private RecordingAnnotation        recAnnotation;
    private IRecordingAreaPresentation presentation;

    @Override
    protected void activateComponent() {
        presentation.addPropertyChangeListener(this);
    }

    @Override
    protected void deactivateComponent() {
        if (recAnnotation != null) {
            recAnnotation.close();
        }
        presentation.removePropertyChangeListener(this);
    }

    public void bindPresentation(IRecordingAreaPresentation service) {
        this.presentation = service;
    }

    public void unbindPresentation(IRecordingAreaPresentation service) {
        this.presentation = service;
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(IRecordingAreaPresentation.ACTIVATE)) {
            if (evt.getNewValue().equals(Boolean.TRUE)) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        recAnnotation = new RecordingAnnotation();
                        recAnnotation.open(presentation.getRecordingRect());
                    }
                });
            } else {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        if (recAnnotation != null) {
                            recAnnotation.close();
                        }
                    }
                });
            }
        }
    }

}
